package com.eati.cm.utils;

import javax.persistence.Id;
import java.lang.reflect.Field;

public class ReflectUtil {
    public static Field getIdField(Class cls){
        Field idField = null;
        for(Field field : cls.getDeclaredFields()){
            if(field.isAnnotationPresent(Id.class)){
                idField = field;
                break;
            }
        }
        if(idField == null) {
            try {
                throw new NoSuchFieldException(
                        "CANNOT FIND BY ID -- CLASS " + cls.getName()
                        + " HAS NO FIELD WITH ID ANNOTATION!");
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        return idField;
    }
}
